import { CommonOrderStatus, DocType } from '../common/docDefinition.js';
import { Util } from '../../utils/util.js';
import { DocBase } from '../common/docBase.js';
import { DateUtil } from '../../utils/dateUtil.js';

export class PeriodSettlementFilter {
    toJson() {
        return {
        };
    }
}

export class PeriodSettlementOrder extends DocBase {
    /** @type {DateTime} 开始时间 */
    _periodStartTime = null;

    /** @type {DateTime} 结束时间 */
    _periodEndTime = null;

    /** @type {int} 最小记录数 <= 0 时全部结算 */
    _minRecordCount = 0;

    get periodStartTime() {
        return this._periodStartTime;
    }
    set periodStartTime(value) {
        this._periodStartTime = value;
    }

    get periodEndTime() {
        return this._periodEndTime;
    }
    set periodEndTime(value) {
        this._periodEndTime = value;
    }

    get minRecordCount() {
        return this._minRecordCount;
    }
    set minRecordCount(value) {
        this._minRecordCount = value;
    }

    /**
 * 销售合同主构造函数
 * @param {Object} params 构造参数
 */
    constructor({
        periodStartTime,
        periodEndTime,

        // super
        id = "",
        storeId = "",
        storeName = "",
        remark = "",
        operator = "",
        operatorId = "",
        docMaker = "",
        status = CommonOrderStatus.Passed,
        title = "",
        docCode = "",
        parentDocType = null,
        parentDocCode = "",
        parentDocTitle = "",
        sourceDocType = null,
        sourceDocCode = "",
        sourceDocTitle = "",
        recordDate = null,
    } = {}) {
        super({
            id,
            title,
            docCode,
            storeId,
            storeName,
            status,
            docType: DocType.PERIOD_SETTLEMENT,
            parentDocType,
            parentDocCode,
            parentDocTitle,
            sourceDocCode,
            sourceDocType,
            sourceDocTitle,
            recordDateTime: recordDate,
            operator,
            operatorId,
            docMaker,
            operateDateTime: recordDate,
            remark
        });

        this._periodStartTime = periodStartTime;
        this._periodEndTime = periodEndTime;
    }

    // 创建空记录
    static empty({
        operator = null,
        operatorId = null,
        storeId = null,
        storeName = null
    } = {}) {
        return new PeriodSettlementOrder({
            operator,
            operatorId,
            storeId,
            storeName
        });
    }

    /**
     * 克隆结算单
     * @param {PeriodSettlementOrder} source
     * @returns {PeriodSettlementOrder}
     */
    static clone(source) {
        let baseOrder = DocBase.clone(source);

        const order = new PeriodSettlementOrder({
            periodStartTime: source._periodStartTime,
            periodEndTime: source._periodEndTime
        });

        Object.assign(order, baseOrder);
        return order;
    }

    /**
    * 从JSON创建结算单
    * @param {Object} json
    * @returns {PeriodSettlementOrder}
    */
    static fromJson(json) {
        let baseOrder = DocBase.fromJson(json, DocType.PERIOD_SETTLEMENT);

        const order = new PeriodSettlementOrder({
            periodStartTime: Util.jsonToDateTime(json['periodStartTime']),
            periodEndTime: Util.jsonToDateTime(json['periodEndTime']),
            minRecordCount: Util.jsonToInt(json['minRecordCount'], 0)
        });

        Object.assign(order, baseOrder);
        return order;
    }

    /**
     * 转换为JSON
     * @returns {Object}
     */
    toJson() {
        const json = super.toJson();
        if (this._periodStartTime) { json.periodStartTime = DateUtil.getDayTitle(this._periodStartTime) }

        if (this._periodEndTime) { json.periodEndTime = DateUtil.getDayTitle(this._periodEndTime) }

        json.minRecordCount = this._minRecordCount;

        return json;
    }
}